/*
 * CreateGameWindow.cpp
 *
 *  Created on: 06/06/2014
 *      Author: lucio
 */

#include "CreateGameWindow.h"
#include <iostream>

void CreateGameWindow::SetNull(){
	this->SelectedCampaingInfo.SetNull();
}

void CreateGameWindow::CreateGame(){
	Gtk::TreeModel::Row Row = *CampaingList->get_selection()->get_selected();
	if(GameNameEntry->get_text() != "" && Row != NULL){
		std::string id = Row->get_value(model.Id).data();
		std::string name = Row->get_value(model.Name).data();
		std::string difficult = Row->get_value(model.Difficult).data();
		std::string levelSize = Row->get_value(model.LevelSize).data();
		std::string minPlayer = Row->get_value(model.MinPlayers).data();
		std::string imgId = Row->get_value(model.ImagePath).data();
		this->SelectedCampaingInfo.setCampaignID(id);
		this->SelectedCampaingInfo.setName(name);
		this->SelectedCampaingInfo.setDifficulty(difficult);
		this->SelectedCampaingInfo.setMinPlayers(minPlayer);
		this->SelectedCampaingInfo.setLevelsSize(levelSize);
		this->SelectedCampaingInfo.setImageID(imgId);
		this->SelectedCampaingInfo.setCreated(true);
		this->gameCreated = true;
		this->GameName = this->GameNameEntry->get_text();
		this->hide();
	}
}

void CreateGameWindow::WindowSignalHide() {
	this->GameNameEntry->set_text("");
	this->PreviewImage->clear();
	this->CampaingList->get_selection()->unselect_all();
}

void CreateGameWindow::ChangePreviewImage() {
	Gtk::TreeModel::Row Row = *CampaingList->get_selection()->get_selected();
	this->PreviewImage->set(Row->get_value(model.ImagePath).data());
}

void CreateGameWindow::AddCampaings() {
	CampaingInfo info("1","Campania1","Facil","5","2","Campaing1.png");
	this->CampaingVector.push_back(info);
	Gtk::TreeModel::Row row = *(this->ListStore->append());
	row[model.Id] = info.getCampaignID();
	row[model.Name] = info.getName();
	row[model.Difficult] = info.getDifficulty();
	row[model.LevelSize] = info.getLevelsSize();
	row[model.MinPlayers] = info.getLevelsSize();
	row[model.ImagePath] = info.getImageID();
}

void CreateGameWindow::CreateCampaingList() {
	this->ListStore = Gtk::ListStore::create(model);
	builder->get_widget("GameCampaingList", this->CampaingList);
	this->CampaingList->set_model(this->ListStore);
	this->CampaingList->append_column("Id", model.Id);
	this->CampaingList->append_column("Name", model.Name);
	this->CampaingList->append_column("Difficult", model.Difficult);
	this->CampaingList->append_column("LevelSize", model.LevelSize);
	this->CampaingList->append_column("MinPlayers", model.MinPlayers);
}

CreateGameWindow::CreateGameWindow(BaseObjectType* object,
		const Glib::RefPtr<Gtk::Builder> &Builder):
			Gtk::Window(object), builder(Builder){
	builder->get_widget("NameEntry",this->GameNameEntry);
	builder->get_widget("CreateGameButton",this->CreateGameButton);

	this->CreateCampaingList();
	this->AddCampaings();
	this->gameCreated = false;
	builder->get_widget("CreatePreviewImage",this->PreviewImage);

	this->CreateGameButton->signal_clicked().connect
				(sigc::mem_fun(*this, &CreateGameWindow::CreateGame));

	this->CampaingList->signal_cursor_changed().connect
			(sigc::mem_fun(*this, &CreateGameWindow::ChangePreviewImage));

	this->signal_hide().connect
			(sigc::mem_fun(*this, &CreateGameWindow::WindowSignalHide));

}

CreateGameWindow::~CreateGameWindow() {
	// TODO Auto-generated destructor stub
}

